const KEY = 'user'

export interface IUser {
    name: string
    routes: string[]
}

export const getUser = (): IUser => {
    return JSON.parse(localStorage.getItem(KEY) || '{}')
}

export const setUser = (newUser: IUser) => {
    window.localStorage.setItem(KEY, JSON.stringify(newUser))
}

export const getRoutes = () => {
    const user = getUser()
    return user.routes || []
}

export const setRoutes = (routes: string[]) => {
    const user = getUser()
    user.routes = routes
    setUser(user)
}

export const getName = () => {
    return getUser().name
}

export const setName = (newName: string) => {
    const user = getUser()
    user.name = newName
    setUser(user)
}
